package com.xuhai.blog19.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuhai.blog19.admin.dao.CommentDao;
import com.xuhai.blog19.admin.entity.CommentEntity;
import com.xuhai.blog19.admin.service.CommentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Author PangJunjie
 * @Date 2023/2/10/010
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentDao, CommentEntity> implements CommentService {
    @Override
    public IPage<CommentEntity> getPage(int page, int limit, CommentEntity comment) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(comment.getCommentTimeRange())) {
            startTime = comment.getCommentTimeRange().split(",")[0];
            endTime = comment.getCommentTimeRange().split(",")[1];
        }
        return this.page(new Page<>(page, limit),
                new QueryWrapper<CommentEntity>()
                        .eq(StringUtils.isNotBlank(comment.getName()), "name", comment.getName())
                        .eq(comment.getCommentStatus() != null, "comment_status", comment.getCommentStatus())
                        .eq(comment.getStatus() != null, "status", comment.getStatus())
                        .eq(comment.getArticleId() != null, "article_id", comment.getArticleId())
                        .between(StringUtils.isNotBlank(comment.getCommentTimeRange()), "comment_time", startTime, endTime).orderByDesc("comment_time")
        );
    }

    @Override
    public void deleteByArticleId(int articleId) {
        this.remove(new QueryWrapper<CommentEntity>().eq("article_id", articleId));
    }
}
